
// 常用正则表达式
var regulars = {
    year: /^(19|20)\d{2}$/,
    number: /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
    bankNum: /^\d{16}|\d{19}$/,
    telephone: /^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$/,
    int: /^[0-9]*$/,
    phone: /^[1][0-9]{10}$/,
    allChinese: /^([\u4E00-\u9FA5]+，?)+$/,
    haveChinese: "[\\u4E00-\\u9FFF]+",
    idCard15: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/,
    idCard18: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$/,
    url: /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/,
    email: /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/,
    Special: ["~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")", ":", ";", "'", "|", "\\", "<", ">", "?", "/", "<<", ">>", "||", "//", "administrators", "administrator", "管理员", "系统管理员", "admin", "select", "delete", "update", "insert", "create", "drop", "alter", "trancate"],
    null: "^[ ]+$",
};

// 正则表达式的对应错误信息语句
var regularsErrorText = {
    year: '只能为年份(四位,1900-2099)',
    number: '只能为数字',
    bankNum: '格式错误',
    telephone: '格式错误',
    int: '只能为正整数',
    phone: '不能为空',
    allChinese: '只能为中文',
    haveChinese: '中含有汉字',
    isIdCard: '格式错误',
    url: '格式错误',
    email: '格式错误',
    isSpecial: '不能包含特殊字符',
    notNull: "不能为空",
};

// 验证用方法集合
var verifyFunctions = {
    default: function (str, type) {
        var re = new RegExp(regulars[type]);
        return re.test(str) ? true : false;
    },
    haveChinese: function (str) {
        var re = new RegExp(regulars.haveChinese);
        return !re.test(str) ? true : false;
    },
    notNull: function (str) {
        if (str == "" || str == undefined || str == null || str == NaN) return false;
        var re = new RegExp(regulars.null);
        return !re.test(str);
    },
    isSpecial: function (str) {
        str = str.toLowerCase();
        for (var i = 0; i < regulars.Special.length; i++) {
            if (str.indexOf(regulars.Special[i]) >= 0) {
                return false;
            }
        }
        return true;
    },
    isIdCard: function (str) {
        var re1 = new RegExp(regulars.idCard15);
        var re2 = new RegExp(regulars.idCard18);
        return re1.test(str) || re2.test(str) ? true : false;
    }
};

var _verify = function () {
    this.regulars = regulars;
    this.regularsErrorText = regularsErrorText;
    this.verifyFunctions = verifyFunctions;
};
_verify.prototype.is = function (type, obj) {
    var result = null;
    var mold = null;
    if (obj) {
        var objectString = Object.prototype.toString.call(obj);
        switch (type) {
            case "string":
                result = objectString == "[object String]";
                break;
            case "function":
                result = objectString == "[object Function]";
                break;
            case "array":
                result = objectString == "[object Array]";
                break;
            case "number":
                result = objectString == "[object Number]";
                break;
            case "date":
                result = objectString == "[object Date]";
                break;
            case "object":
                result = objectString == "[object Object]";
                break;
            case "bool":
                result = objectString == "[object Boolean]";
                break;
            case "regExp":
                result = objectString == "[object RegExp]";
                break;
        };
        return result;
    } else {
        var objectString = Object.prototype.toString.call(type);
        switch (objectString) {
            case "[object String]":
                mold = "string";
                break;
            case "[object Function]":
                mold = "function";
                break;
            case "[object Array]":
                mold = "array";
                break;
            case "[object Number]":
                mold = "number";
                break;
            case "[object Date]":
                mold = "date";
                break;
            case "[object Object]":
                mold = "object";
                break;
            case "[object Boolean]":
                mold = "bool";
                break;
            case "[object RegExp]":
                mold = "regExp";
                break;
        };
        return mold;
    };
};
_verify.prototype.jude = function (type, value) {
    var SpecialArray = ['notNull', 'isSpecial', 'isIdCard', 'haveChinese'];
    if ($.inArray(type, SpecialArray) != -1) {
        var answer = verifyFunctions[type](value);
        return {
            answer: answer,
            errorText: (answer ? null : regularsErrorText[type])
        };
    } else if (type.indexOf('$') != -1) {
        var answer = this.custom(type.split('$'), value);
        return {
            answer: answer.result,
            errorText: answer.text
        };
    } else {
        var answer = verifyFunctions.default(value, type);
        return {
            answer: answer,
            errorText: (answer ? null : regularsErrorText[type])
        };
    };
};
_verify.prototype.inArray = function (str, _array) {
    if ($.inArray(str, _array) == -1) {
        return false
    } else {
        return true
    }
};
_verify.prototype.isEqual = function (a, b) {
    if (a === b) {
        return a !== 0 || 1 / a === 1 / b;
    };
    if (a == null || b == null) {
        return a === b;
    };
    var A = Object.prototype.toString.call(a);
    var B = Object.prototype.toString.call(b);
    if (A !== B) {
        return false;
    };
    switch (A) {
        case '[object RegExp]':
        case '[object String]':
            return '' + a === '' + b;
        case '[object Number]':
            if (+a !== +a) {
                return +b !== +b;
            };
            return +a === 0 ? 1 / +a === 1 / b : +a === +b;
        case '[object Date]':
        case '[object Boolean]':
            return +a === +b;
    };
    if (A == '[object Object]') {
        if (JSON.stringify(a) != JSON.stringify(b)) {
            return false;
        };
        return true;
    };
    if (A == '[object Array]') {
        if (a.toString() == b.toString()) {
            return true;
        }
        return false;
    };
};
_verify.prototype.custom = function (type, str) {
    var name = type[0];
    if (name == 'length') {
        if (type[1].indexOf('les') != -1) {
            var nums = type[1].replace('les', '');
            nums = parseInt(nums);
            var length = str.toString().length;
            return {
                result: length >= nums ? true : false,
                text: '不能小于' + nums + '位'
            };
        } else if (type[1].indexOf('gre') != -1) {
            var nums = type[1].replace('gre', '');
            nums = parseInt(nums);
            var length = str.toString().length;
            return {
                result: length <= nums ? true : false,
                text: '不能大于' + nums + '位'
            }
        };
    };
};

export const verify = new _verify();